#################################################################################
#################################################################################

###                   R code to reproduce Figures B1-B3 in                    ### 
###                 Supplementary materials (Appendix B) to                   ###
###  "The Index of Emancipative Values: Measurement Model Misspecifications"  ###

#################################################################################
#################################################################################




setwd("C:/Documents and Settings/User")

#################################################################################

### FIGURE B1 ###


tiff('Figure B1.tiff', units = "cm", width = 24, height = 12.5, res = 600)

par(mar = c(.1,.25,.1,.1), mfrow = c(1,2))


### METRIC NON-INVARIANCE ###

plot(rnorm(1000,0, 1), xlim =c(-0.5,3), ylim = c(-.5,6), type = 'n',  axes = F)
### ARROWS ###
# Regression lines
arrows(0, 1, 2.5, 2.25, length = 0,lwd = 2.5)
arrows(0, 1.5, 2.5, 4.5, length = 0, lty = 3.5, lwd = 2.5)
# Axes
arrows(0, 0, 0, 5.75, lwd = 2.5)
arrows(0, 0, 3, 0, lwd = 2.5)
# Values of the latent trait
arrows(1, 0, 1, 2.7, length = 0, lty = 2, lwd=2)
arrows(2, 0, 2, 3.9, length = 0, lty = 5, lwd=2)
# Corresponding values of the observed item in the 1st group
arrows(1, 1.5, 0, 1.5, length = 0, lty = 2, lwd=2)
arrows(2, 2, 0, 2, length = 0, lty = 5, lwd=2)
# Corresponding values of the observed item in the 2nd group
arrows(1, 2.7, 0, 2.7, length = 0, lty = 2, lwd=2)
arrows(2, 3.9, 0, 3.9, length = 0, lty = 5, lwd=2)
### POINTS ###
points(1, 0, cex =1, pch = 16)
points(2, 0, cex =1, pch = 16)
points(0, 1, cex =1, pch = 16)
points(0, 1.5, cex =1, pch = 16)
points(0, 2, cex =1, pch = 16)
points(0, 2.7, cex =1, pch = 16)
points(0, 3.9, cex =1, pch = 16)
points(1, 1.5, cex =1, pch = 16)
points(2, 2, cex =1, pch = 16)
points(1, 2.7, cex =1, pch = 16)
points(2, 3.9, cex =1, pch = 16)
### TEXT ###
# X-axis
text(1, -.2, cex = 1.2, expression(paste(eta, " = 1")))
text(2, -.2, cex = 1.2, expression(paste(eta, " = 2")))
text(3.05, -.25, cex = 1.5, expression(eta))
text(2.5, -.6, cex = 1.2,"Latent Variable")
# Y-axis 
text(-.2, 5.65, cex = 1.5, expression(y[1]))
text(0.45, 6, cex = 1.2, "Observed item")
text(-.15, .8, cex = 1.2, expression(nu[12]))
text(-.15, 1.3, cex = 1.2, expression(nu[11]))
# Groups
text(2.55, 2.5, cex = 1.2,"Group 2")
text(2.55, 4.75, cex = 1.2,"Group 1")
### UNDERESTIMATION ###
# Eta = 1
arrows(-0.1, 2.7, -0.2, 2.7, length = 0, lwd=1)
arrows(-0.15, 2.7, -.15, 1.5, length = 0.1, lwd=1)
text(-.4, 2.3, cex =.75, "under-")
text(-.4, 2.1, cex =.75, "estimation")
text(-.4, 1.9, cex =.75, expression(paste("for ", eta, " = 1")))
# Eta = 2
arrows(0, 3.9, -0.1, 3.9, length = 0, lwd=1)
arrows(-.05, 3.9, -.05, 2, length = 0.1, lwd=1)
text(-.3, 3.3, cex =.75, "under-")
text(-.3, 3.1, cex =.75, "estimation")
text(-.3, 2.9, cex =.75, expression(paste("for ", eta, " = 2")))
### EXPRESSION ###
text(1.55, 5.4, cex = 1.5,
expression(lambda[11] > lambda[12]))
text(2.45, 5.4, cex = 1.5,
expression(nu[11] > nu[12]))



### SCALAR NON-INVARIANCE ###

plot(rnorm(1000,0, 1), xlim =c(-0.5,3), ylim = c(-.5,6), type = 'n',  axes = F)
### ARROWS ###
# Regression lines
arrows(0, 1, 2.5, 2.25, length = 0,lwd = 2.5)
arrows(0, 2.5, 2.5, 3.75, length = 0, lty = 3.5, lwd = 2.5)
# Axes
arrows(0, 0, 0, 5.75, lwd = 2.5)
arrows(0, 0, 3, 0, lwd = 2.5)
# Value of the latent trait
arrows(2, 0, 2, 3.5, length = 0, lty = 5, lwd=2)
# Corresponding values of the observed item in the 2nd group
arrows(2, 2, 0, 2, length = 0, lty = 5, lwd=2)
# Corresponding values of the observed item in the 2nd group
arrows(2, 3.5, 0, 3.5, length = 0, lty = 5, lwd=2)
### POINTS ###
points(2, 0, cex =1, pch = 16)
points(0, 1, cex =1, pch = 16)
points(0, 2, cex =1, pch = 16)
points(0, 2.5, cex =1, pch = 16)
points(0, 3.5, cex =1, pch = 16)
points(2, 2, cex =1, pch = 16)
points(2, 3.5, cex =1, pch = 16)
### TEXT ###
# X-axis
text(2, -.2, cex = 1.2, expression(eta[ij]))
text(3.05, -.25, cex = 1.5, expression(eta))
text(2.5, -.6, cex = 1.2,"Latent Variable")
# Y-axis 
text(-.2, 5.65, cex = 1.5, expression(y[1]))
text(0.45, 6, cex = 1.2, "Observed item")
text(-.175, .8, cex = 1.2, expression(nu[12]))
text(-.175, 2.3, cex = 1.2, expression(nu[11]))
# Groups
text(2.55, 2.5, cex = 1.2,"Group 2")
text(2.55, 4, cex = 1.2,"Group 1")
### UNDERESTIMATION ###
arrows(0, 3.5, -0.1, 3.5, length = 0, lwd=1)
arrows(-.05, 3.5, -.05, 2, length = 0.1, lwd=1)
text(-.3, 3.0, cex =.75, "under-")
text(-.3, 2.8, cex =.75, "estimation")
text(-.3, 2.6, cex =.75, expression(paste("for all ", eta)))
### EXPRESSION ###
text(1.55, 5.4, cex = 1.5,
expression(lambda[11] == lambda[12]))
text(2.45, 5.4, cex = 1.5,
expression(nu[11] > nu[12]))


dev.off()



#################################################################################

###                                 FIGURE B2                                  ###

#################################################################################

tiff('Figure B2.tiff', units = "cm", width = 24, height = 12.5, res = 600)
par(mar = c(.1,.5,.1,.5), mfrow = c(1,2))


### Non-Informative Prior

plot(rnorm(0,1, 1000), xlim =c(-3,3), ylim = c(-0.5,2),type = 'n',  axes = F)
# X-Axis
arrows(-3, 0, 3, 0, length = 0, lwd = 3)
# Prior Belief about the Difference
arrows(0, -0.05, 0, 0.05, length = 0, lwd = 3)
# Uniform Prior
arrows(-2.95, 0, -2.95, 0.15, length = 0, col = "gray75",lwd = 2)
arrows(2.95, 0, 2.95, 0.15, length = 0, col = "gray75",lwd = 2)
arrows(-2.95, 0.15, 2.95, 0.15, length = 0, col = "gray75",lwd = 2)
# Likelihood
curve(dnorm(x, mean=1.5, sd=.3), lwd=2, lty = 1, add=TRUE, yaxt="n")
# Posterior
curve(dnorm(x, mean=1.5, sd=.28), lwd=3, lty = 5, add=TRUE, yaxt="n")

# Annotations
text(0, -.15, cex = 1.5, expression(0))
text(3, -.15, cex = 2, expression(paste("+")))
text(-2.95, -.15, cex = 2, expression(paste("-")))
text(1.45, -.1, cex = 1.75, expression(paste(delta, "'")))
text(0, 2, cex = 1.35, "Non-Informative Prior")
text(-1.5, 0.25, cex = 1.2, col = "gray75", "Prior distribution")
text(0.15, 1.5, cex = 1.2, "Posterior estimate")
text(2.45, 1, cex = 1, "Unconstraint\n likelihood\n of the \n observed \n difference")


#################################################################################
### Informative Prior

plot(rnorm(0,1, 1000), xlim =c(-3,3), ylim = c(-0.5,2),type = 'n',  axes = F)
# Prior Belief about the Difference
arrows(0, -0.05, 0, 0.05, length = 0, lwd = 3)
# Informative Prior 
curve(dnorm(x, mean=0, sd=.21), lwd = 2, col = "gray75",add=TRUE, yaxt="n")
# Likelihood
curve(dnorm(x, mean=1.5, sd=.3), lwd=2, lty = 1, add=TRUE, yaxt="n")
# Posterior
curve(dnorm(x, mean=.25, sd=.235), lwd=3, lty = 5, add=TRUE, yaxt="n")
arrows(-3, 0, 3, 0, length = 0, lwd = 3)
# X-Axis

# Annotations
text(0, 2, cex = 1.35, "Informative Prior")
text(0, -.15, cex = 1.5, expression(0))
text(3, -.15, cex = 2, expression(paste("+")))
text(-2.95, -.15, cex = 2, expression(paste("-")))
text(.3, .1, cex = 1.75, expression(paste(delta, "'")))
text(-.9, 1.75, cex = 1.2, col = "gray75", "Prior \ndistribution")
text(1.1, 1.6, cex = 1.2, "Posterior \nestimate")
text(2.45, 1, cex = 1, "Unconstraint\n likelihood\n of the \n observed \n difference")

dev.off()

#################################################################################

###                                 FIGURE B3                                 ###

#################################################################################

tiff('Figure B3.tiff', units = "cm", width = 24, height = 12.5, res = 600)
par(mar = c(.1,1,.1,1), mfrow = c(1,2))

### Classical Approach

plot(rnorm(0,1, 1000), xlim =c(-1,1), ylim = c(-0.5,3),type = 'n',  axes = F)
# X-Axis
arrows(-1, 0, 1, 0, length = 0, lwd = 2.5)
# Prior Belief about the Difference
arrows(0, 0, 0, 2.5, length = 0, lwd = 2.5, lty = 2)
# Annotations
text(0, 2.65, cex = 1.5, expression(delta == 0))
text(1, -.15, cex = 2, expression(paste("+")))
text(-.99, -.15, cex = 2, expression(paste("-")))
text(1, .15, cex = 2, expression(delta))
text(0, 2.9, cex = 1.75, "Classical Approach")

### Approximate Bayesian Approach

plot(rnorm(1000,0, .5), xlim =c(-1,1), ylim = c(-0.5,3),type = 'n',   axes = F)
# X-Axis
arrows(-1, 0, 1, 0, length = 0, lwd = 2.5)
# Prior Belief about the Difference
arrows(0, 0, 0, 2.5, length = 0, lwd = 2.5, lty = 2)
# Annotations
text(0, 2.65, cex = 1.5, expression(delta%in%N(0, 0.05)))
text(1, -.15, cex = 2, expression(paste("+")))
text(-.99, -.15, cex = 2, expression(paste("-")))
text(1, .15, cex = 2, expression(delta))
text(0, 2.9, cex = 1.75, "Approximate Bayesian Approach")
# Difference Distribution
curve(dnorm(x, mean=0, sd=.16), lwd=2, lty = 1, add=TRUE, yaxt="n")

dev.off()

#q()